home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-apt / examples / config.py < prev    next >
Encoding:
Python Source  |  2009-03-30  |  2.2 KB  |  59 lines

  1. #!/usr/bin/python
  2. # Example demonstrating how to use the configuration/commandline system
  3. # for configuration.
  4. # Some valid command lines..
  5. #   config.py -h --help            ; Turn on help
  6. #   config.py -no-h --no-help --help=no  ; Turn off help
  7. #   config.py -qqq -q=3            ; verbosity to 3
  8. #   config.py -c /etc/apt/apt.conf ; include that config file]
  9. #   config.py -o help=true         ; Turn on help by giving a
  10. #                                  ; config file string
  11. #   config.py -no-h -- -help       ; Turn off help, specify the file '-help'
  12. # -c and -o are standard APT-program options.
  13.  
  14. # This shows how to use the system for configuration and option control.
  15. # The other varient is for ISC object config files. See configisc.py.
  16. import apt_pkg
  17. import sys
  18. import posixpath
  19.  
  20. # Create a new empty Configuration object - there is also the system global
  21. # configuration object apt_pkg.Config which is used interally by apt-pkg
  22. # routines to control unusual situations. I recommend using the sytem global
  23. # whenever possible..
  24. Cnf = apt_pkg.newConfiguration()
  25.  
  26. print "Command line is", sys.argv
  27.  
  28. # Load the default configuration file, InitConfig() does this better..
  29. Cnf.Set("config-file", "/etc/apt/apt.conf")  # or Cnf["config-file"] = ".."
  30. if posixpath.exists(Cnf.FindFile("config-file")):
  31.     apt_pkg.ReadConfigFile(Cnf, "/etc/apt/apt.conf")
  32.  
  33. # Merge the command line arguments into the configuration space
  34. Arguments = [('h', "help", "help"),
  35.              ('v', "version", "version"),
  36.              ('q', "quiet", "quiet", "IntLevel"),
  37.              ('c', "config-file", "", "ConfigFile"),
  38.              ('o', "option", "", "ArbItem")]
  39. print "FileNames", apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
  40.  
  41. print "Quiet level selected is", Cnf.FindI("quiet", 0)
  42.  
  43. # Do some stuff with it
  44. if Cnf.FindB("version", 0) == 1:
  45.     print "Version selected - 1.1"
  46.  
  47. if Cnf.FindB("help", 0) == 1:
  48.     print "python-apt", apt_pkg.Version, \
  49.           "compiled on", apt_pkg.Date, apt_pkg.Time
  50.     print "Hi, I am the help text for this program"
  51.     sys.exit(0)
  52.  
  53. print "No help for you, try -h"
  54.  
  55. # Print the configuration space
  56. print "The Configuration space looks like:"
  57. for I in Cnf.keys():
  58.     print "%s \"%s\";" % (I, Cnf[I])
  59.